﻿package bipede.membres
{
	import fab.geom.Cone;
	import fab.geom.Drawer;
	import fab.geom.Line;
	import fab.geom.Circle;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.events.Event;
	import drawer.events.DrawEvent;
	
	public class Yeux extends MovieClip//Squelette 
	{
		private var cone:Cone;
		public var regard:Line;
		private var circleG:Circle;
		private var circleD:Circle;
		public function Yeux() 
		{
			//blancG
			//blancD
			//pupilleG
			//pupilleD
			circleG = new Circle( new Point( blancG.x, blancG.y ), .7 * blancG.width / 2 );
			circleD = new Circle( new Point( blancD.x, blancD.y ), .7 * blancD.width / 2 );
			
			blancD.paupiere_sup.stop();
			blancD.paupiere_inf.stop();
			blancG.paupiere_sup.stop();
			blancG.paupiere_inf.stop();
			
			addEventListener(Event.ADDED, frameConstructedHandler);
		}		
		private function frameConstructedHandler(event:Event):void
		{
			cligne( false, false );
		}
		public function update( p:Point ):void
		{
			var centre:Point = localToGlobal( new Point( 0,0 ) );
			var angle:Number = Math.atan2( p.y - centre.y, p.x - centre.x );
			
			regard = new Line( centre, angle, "polaire" );
			
			cone = new Cone( centre, 500, angle-Math.PI / 10, angle + Math.PI / 10 );
			
			place_pupilles( circleG.projection( globalToLocal( p ), true ), circleD.projection( globalToLocal( p ), true ) );
			
			//dispatchEvent( new DrawEvent( DrawEvent.DRAWCURVE, cone.line1, 0xFF0000 ) );
			//dispatchEvent( new DrawEvent( DrawEvent.DRAWPOINT, p, 0x0000FF ) );
		}
		public function mouse_move(event:MouseEvent):void 
		{
			update( new Point( event.stageX, event.stageY ) );
			
			if (Math.random() > .99 ) cligne();
		}		
		public function place_pupilles( g:Point, d:Point ):void 
		{
			pupilleG.x = g.x;
			pupilleG.y = g.y;
			pupilleD.x = d.x;
			pupilleD.y = d.y;
		}			
		public function cligne( g:Boolean=true, d:Boolean=true ):void 
		{
			if ( g && blancG.paupiere_sup.currentFrame == 1 )
			{
				blancG.paupiere_sup.gotoAndPlay(1);
				blancG.paupiere_inf.gotoAndPlay(1);
			}
			if ( d && blancD.paupiere_sup.currentFrame == 1 )
			{
				blancD.paupiere_sup.gotoAndPlay(1);
				blancD.paupiere_inf.gotoAndPlay(1);
			}
		}			
	}
}